package com.b2s.scrumblr.kb.core.mail;

import com.b2s.scrumblr.kb.model.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.integration.mail.ImapMailReceiver;
import org.springframework.integration.mail.MailReceiver;
import org.springframework.stereotype.Component;

import javax.mail.*;
import javax.mail.internet.MailDateFormat;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.UUID;

/**
 * Created by Administrator on 2016/8/4.
 */
public class EmailReceiver {
    private static Properties properties = null;
    static{
        properties = System.getProperties();
        properties.setProperty("mail.store.protocol", "imaps");
    }

    public static List<Article> getEmails(int readMailListNum) throws MessagingException, IOException {
        Session session = Session.getInstance(properties, null);

        Store store = session.getStore();
        store.connect("imap.symbio.com", "xiaobing.zheng@symbio.com", "p39nA6wf");

        Folder folder = store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);

        int totalMail = folder.getMessageCount();

        readMailListNum = readMailListNum > totalMail? totalMail : readMailListNum;

        List<Article> articles = new ArrayList<Article>(readMailListNum);
        for(int i = 1; i < readMailListNum; i++){
            Message message = folder.getMessage(i);
            Multipart multipart = (Multipart)message.getContent();
            Article article = new Article();
            article.setId(UUID.randomUUID().toString());
            article.setTitle(message.getSubject());
            article.setContent(multipart.getBodyPart(0).getContent().toString());
            articles.add(article);
        }
        return articles;
    }

    public static void main(String[] args) throws MessagingException, IOException {

        List<Article> articles = getEmails(3);
        if(articles != null && !articles.isEmpty()){
            for(Article article : articles){
                System.out.println(article.getTitle());
                System.out.println(article.getContent());
                System.out.println("***************************************");
            }
        }
       /* //1.定义协议
        Properties properties = System.getProperties();
        properties.setProperty("mail.store.protocol", "imaps");

        //2.得到一个session实例为read mail
        Session session = Session.getInstance(properties, null);

        //3.访问mail通过store
        Store store = session.getStore();
        store.connect("imap.symbio.com", "xiaobing.zheng@symbio.com", "p39nA6wf");
        Folder folder = store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);

        System.out.println("未读邮件："+folder.getUnreadMessageCount());*/
       /* //4.读inbox
        Message message = folder.getMessage(1);



        Address[] addresses = message.getFrom();
        for(Address address : addresses){
            System.out.println("FROM:" + address.toString());
        }

        Multipart mp = (Multipart)message.getContent();
        BodyPart bp = mp.getBodyPart(0);

        System.out.println("SENT DATE:" + message.getSentDate());
        System.out.println("SUBJECT:" + message.getSubject());
        System.out.println("CONTENT:" + bp.getContent());*/

    }
}
